
var userloginlink = "/hoaservice/lc_user/userloginlink";//用户登录
var userregisterlink = "/hoaservice/lc_user/userregisterlink";//用户注册
var userinitlink = "/hoaservice/lc_user/userinitlink";//验证用户是否登录
var userlogoutlink = "/hoaservice/lc_user/userlogoutlink";//用户注销


var inittranslate = "/hoaservice/zyyfanyiapi/inittranslate";//数据翻译
var inittranslate_status = "/hoaservice/zyyfanyiapi/inittranslate_status";//数据翻译-状态刷新


var shuyufanyilink = "/hoaservice/zyyfanyiapi/shuyufanyilink";//术语翻译

var upload_file = "/hoaservice/file/upload_file"; //上传文件
var delAttached_link = "/hoaservice/doc/delAttached_link"; //删除附件
var initfanyilink = "/hoaservice/zyyfanyiapi/initfanyilink";//执行翻译
var initfanyistatuslink = "/hoaservice/zyyfanyiapi/initfanyistatuslink";//执行翻译刷新状态
var uploader = "";


$(function () {

    $("#searchcontent").val("");
    $("#_top_2_sy").css("background-color","#82ACBB");
    $("#result_content").html("");
    $("#fy_content").html("中医药事业是我国医药卫生事业的重要组成部分。国家大力发展中医药事业，实行中西医并重的方针，建立符合中医药特点的管理制度，充分发挥中医药在我国医药卫生事业中的作用。易筋经");
    //$("#_top_2_wdcb_content").hide();

});

/***
 * 导航内容
 * @param _this
 */
function tab_dh(_this) {
    $("#_top_2_ul").find("li").css("background-color","rgb(70, 135, 170)");
    $(_this).css("background-color","#82ACBB");
    $("._center").hide();
    var contentids = "#"+$(_this).prop("id")+"_content";
    $(contentids).show();
    var ids = $(_this).prop("id");
    if(ids=="_top_2_wdcb"){
        $("#_top_2_wdcb_content").show();
        loadUplad();


    }

}

/***
 * 术语翻译
 */
function shuyufanyi() {
    var searchcontent = $.trim($("#searchcontent").val());
    if(searchcontent!=""){
        $.ajax({
            type:"post",
            url:shuyufanyilink,
            data:{
                searchcontent:searchcontent
            },
            cache:false,
            async:false,
            dataType:"json",
            success:function (_info) {
                console.log(_info);
                if(_info.code=="1"){
                   var datalist = _info.data;
                   var htmlstr = "";
                   htmlstr+=" <tr>\n" +
                                "<td>"+datalist.zhongwen+"</td>\n" +
                                "<td>"+datalist.yingwen+"</td>\n" +
                                "<td>"+datalist.miaoshu+"</td>\n" +
                                "<td>"+datalist.laiyuan+"</td>\n" +
                            "</tr>";
                   $("#shuyuresult_content").html(htmlstr);
                }else {
                    $("#shuyuresult_content").html("系统没有收录该术语");
                }
            },
            error:function () {
                alert("网络连接错误");
            }
        });
    }else {
        $.alert("请输入检索词");
    }
}
/****
 * 执行翻译
 */
function initfanyi(_this) {

    $(_this).prop("disabled",true);
    var timestamp = (new Date()).valueOf();
    var randomstr = timestamp+"ST"+Math.ceil(Math.random()*9999999); //随机数


    $("#result_content").html("");
    var from_tran = $("#index_from_tran").val();
    var to_tran = $("#index_to_tran").val();
    var fycontent = $.trim($("#fy_content").text());
    alert(fycontent);

    if(fycontent=="" || fycontent==null){
        alert("源语言不能为空");
        return false;
    }
    if(fycontent.length>2000){
        alert("翻译字数不能超过2000");
        return false;
    }
    if(from_tran==to_tran) {
        //$("#result_content").val(fycontent);
        return false;
    }
    //记时
    $("#_top_2_sy_content_zhuangtai").show();
    var isjishi = setInterval(function () {
        funinittranslate_status(randomstr);
    },1000);


    $.ajax({
        url:inittranslate,
        data:{
            from_tran:from_tran,
            to_tran:to_tran,
            fycontent:fycontent,
            randomstr:randomstr
        },
        type:"post",
        cache:false,
        async:true,
        dataType:"json",
        success:function(content){
            $(_this).prop("disabled",false);
            $("#_top_2_sy_content_zhuangtai").hide();
            window.clearInterval(isjishi);

            if (content.code=="1"){
                var result = content.result;
                var returnlist = content.returnlist;

                //$("#result_content").val(result);
                //翻译结果
                var resultcontentarray = content.resultcontent;
                var resultcontent_cnarray = content.resultcontent_cn;
                var hshtml = "";
                var cnhshtml = "";
                for(var i in resultcontentarray){
                    var cont = resultcontentarray[i];
                    var cn_cont = resultcontent_cnarray[i];

                    hshtml += "<span onmouseover='xfcont(this,"+i+")' onmouseout='xfcont_out(this,"+i+")' >"+cont+"</span>";
                    cnhshtml += "<span id='"+i+"_yuanwen_'>"+cn_cont+"</span>";
                }
                $("#result_content").html(hshtml);
                $("#fy_content").html(cnhshtml);
                //重点词汇
                var sthtmlstr = "";
                for(var i in returnlist){
                   var returnobj = returnlist[i];
                    sthtmlstr+="<tr>\n" +
                                   "<td>"+returnobj.zhongwen+"</td>\n" +
                                   "<td>"+returnobj.yingwen+"</td>\n" +
                                   "<td>"+returnobj.laiyuan+"</td>\n" +
                               "</tr>";
                }
                $("#zhongdiancihui_content").html(sthtmlstr);

            }
        }
    });
}

function xfcont(_this,i) {
    $(_this).css("background-color","#F3F781");
    $(_this).css("padding-top","5px");
    $(_this).css("padding-bottom","5px");
    var ids = "#"+i+"_yuanwen_";
    $(ids).css("background-color","#F3F781");
}
function xfcont_out(_this,i) {
    $(_this).css("background-color","initial");
    $(_this).css("padding-top","0");
    $(_this).css("padding-bottom","0");
    var ids = "#"+i+"_yuanwen_";
    $(ids).css("background-color","initial");
}

/***
 * 执行翻译-状态
 * @param randomstr
 */
function funinittranslate_status(randomstr) {
    $.get(inittranslate_status,{
        randomstr:randomstr
    },function (_info) {
        $("#_top_2_sy_content_zhuangtai_text").text(_info.data);
    },"json");
}



/**
 * 用户注销
 */
function userlogout() {
    $.confirm({
        title:"提示",
        content:"您确定退出吗？",
        buttons:{
            '确定':function () {
                var usertoken = sessionStorage.getItem("usertoken");
                if(usertoken!=undefined){
                    $.get(userlogoutlink,{
                        usertoken:usertoken
                    },function (content) {
                        if(content.code=="1"){
                            sessionStorage.removeItem("usertoken");
                            window.location.reload();
                        }
                    });
                }
            },
            '取消':function () {
                
            }
        }
    });


}
/***
 * 验证用户是否登录
 */
function userinit() {
    var usertoken = sessionStorage.getItem("usertoken");
    if(usertoken==undefined){
        $("#user_caozuo").show();
        $("#user_info").hide();
        return false;
    }
    if(usertoken==null){
        $("#user_caozuo").show();
        $("#user_info").hide();
        return false;
    }
    if(usertoken.length>0){
        $.get(userinitlink,{
            usertoken:usertoken
        },function (content) {
            if(content.code=="1"){
                $("#user_info_name").html(content.data.username);
                $("#user_info").show();
                $("#user_caozuo").hide();
            }else {
                $.alert("用户验证失败，请重新登录","提示");
                $("#user_caozuo").show();
                $("#user_info").hide();
            }
        });
    }else {
        $("#user_caozuo").show();
        $("#user_info").hide();
    }
}
/***
 * 用户注册
 */
function userRegister() {
    var register_username = $.trim($("#register_username").val());
    var register_userpwd = $.trim($("#register_userpwd").val());
    var register_userpwd_2 = $.trim($("#register_userpwd_2").val());
    if(register_username==""){
        $.alert("请输入用户名");
        return false;
    }
    if (register_userpwd==""){
        $.alert("请输入密码");
        return false;
    }
    if (register_userpwd_2==""){
        $.alert("请输入确认密码");
        return false;
    }
    if(register_userpwd!=register_userpwd_2){
        $.alert("用户名和密码不一致");
        return false;
    }
    $.post(userregisterlink,{
        register_username:register_username,
        register_userpwd:register_userpwd
    },function (content) {
        if(content.code=="1"){
            $.alert(content.msg,"提示");
            $("#userregister_modal").modal('hide');
        }else {
            $.alert(content.msg,"提示");
        }
    });


}
/***
 * 用户注册弹出框
 */
function userRegisterModal() {
    $("#userregister_modal").modal('show');
    $("#register_username").val("");
    $("#register_userpwd").val("");
    $("#register_userpwd_2").val("");
}


/***
 * 用户登录弹出框
 */
function userLoginModal() {
    $("#userlogin_modal").modal('show');
    $("#username").val("");
    $("#userpwd").val("");
} 
/***
 * 用户登录
 */
function userLogin() {
    var username = $.trim($("#username").val());
    var userpwd = $.trim($("#userpwd").val());
    if(username!="" && userpwd!=""){
        $.post(userloginlink,{
            user_denglu_name:username,
            user_denglu_pwd:userpwd
        },function (content) {
            if (content.code=="1"){
                sessionStorage.setItem("usertoken",content.data.usertoken);
                $.alert({
                    title:"提示",
                    content:content.msg,
                    autoClose:'确定|2000',
                    buttons:{
                        '确定':{
                            action:function () {
                                window.location.reload();
                            }
                        }
                    }
                });
            }else {
                $.alert(content.msg,"提示");
            }
        });
    }else {
        $.alert("请填写用户名或者密码！","提示");
    }
}



//文档翻译-------------------------------------------------------------------------

/****
 *  执行翻译
 * */
function initFy() {
    var att_name_list = "";
    $("input[name='att_name']").each(function (_index,_this) {
        att_name_list+=","+_this.value;
    });
    if(att_name_list==""){
        alert("请上传翻译文件");
        return false;
    }

    var isjishi = setInterval(function () {
        get_initstatus();
    },3000);

    $.ajax({
        url:initfanyilink,
        data:{
            att_name_list:att_name_list
        },
        type:"post",
        cache:false,
        async:true,
        dataType:"json",
        success:function(content){
            console.log(content);
            window.clearInterval(isjishi);
            if(content.code=="1"){
                var htmlstr = " <tr>\n" +
                    "<td><a href='"+content.data+"'>"+content.dataname+"</a></td>\n" +
                    "<td>"+content.datasize+"</td>\n" +
                    "</tr>";
                $("#result_fanyi_content").html(htmlstr);
            }else {

                alert(content.msg);
            }
        }
    });

}
/***
 * 刷新执行状态
 * */
function get_initstatus(_random) {
    $.ajax({
        url:initfanyistatuslink,
        type:"post",
        cache:false,
        async:true,
        dataType:"json",
        success:function(_info){
            if(_info.code=="1"){
                $("#zhixingguocheng_cont").html(_info.msg);
            }
        }
    });
}
/***
 * 批量导入规则（加载上传文件）
 */
function  loadUplad() {

    $("#thelist_uploadfile_gzj").html("");
    $("#thelist_gzj").html("");
    if(uploader==""){



    uploader = WebUploader.create({
        swf: '../webuploader-0.1.5/Uploader.swf',
        auto:true,
        server: upload_file,
        pick: '#picker_gzj',
        resize: false,
        fileNumLimit:1,
        formData:{
            filetype:10
        },
        accept:{
            title: '文本内容',
            extensions: 'xlsx',
            mimeTypes: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        },
        fileSizeLimit:10485760000
    });

    uploader.on( 'fileQueued', function( file ) {

        $("#thelist_gzj").html( '<div id="' + file.id + '" class="item">' +
            '<h4 class="info">' + file.name + '</h4>' +
            '<p class="state">等待上传...</p>' +
            '</div>' );
    });
    uploader.on( 'beforeFileQueued', function( file ) {
        uploader.reset();
    });

    uploader.on( 'uploadProgress', function( file, percentage ) {
        var $li = $( '#'+file.id ),
            $percent = $li.find('.progress .progress-bar');

        if ( !$percent.length ) {
            $percent = $('<div class="progress progress-striped active">' +
                '<div class="progress-bar" role="progressbar" style="width: 0%">' +
                '</div>' +
                '</div>').appendTo( $li ).find('.progress-bar');
        }

        $li.find('p.state').text('上传中并正在处理，请稍等！！！');
        $percent.css( 'width', percentage * 100 + '%' );
    });
    //上传成功
    uploader.on( 'uploadSuccess', function( file,response ) {
        $( '#'+file.id ).find('p.state').text('上传完毕');
        setTimeout(function(){
            $("#thelist").html("");
            if(response.code=='1'){
                var htmlstr = "<tr id=\"att"+response.data.attachedid+"\" >"+
                    "<td>"+response.data.truename+"<input name='att_name' value='"+response.data.attachedid+"' style='display: none;' /></td>"+
                    "<td>"+response.data.size+"</td>"+
                    "<td><span class=\"label label-danger\" style='cursor: pointer;'  onclick=\"delAttached('"+response.data.attachedid+"')\">删除</span></td>"+
                    "</tr>";

                $("#thelist_uploadfile_gzj").append(htmlstr);
            }
            $("#thelist_gzj").html("");
            uploader.reset();
        },1000);
    });
    uploader.on( 'uploadError', function( file ) {
        $( '#'+file.id ).find('p.state').text('上传出错');
        uploader.reset();
    });
    uploader.on( 'uploadComplete', function( file ) {
        $( '#'+file.id ).find('.progress').fadeOut();
    });

    }


}

/***
 *  删除附件
 */
function delAttached(attachedid) {
    $.confirm({
        title:"提示",
        content:"您确认删除此附件吗？",
        buttons:{
            "确定":function () {
                $.get(delAttached_link,{attachedid:attachedid},function (msg) {
                    if (msg.code=='1'){
                        $("#att"+attachedid).remove();
                        $.confirm({
                            title: '提示',
                            content: '成功删除附件！',
                            autoClose: '确定|2000',
                            buttons: {
                                确定: {
                                }
                            }
                        });
                    }
                });
            },
            "取消":function () {

            }
        }
    });
}
